package com.hzcx.app.simplechat.ui.publicui;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseDialog;
import com.hzcx.app.simplechat.ui.publicui.bean.UpdateVersionBean;
import com.hzcx.app.simplechat.util.log.LogUtils;
import com.hzcx.app.simplechat.view.MainBlueConfirmButton;

import butterknife.BindView;

/**
 * 版本更新弹窗
 */
public class UpdateVersionDialog extends BaseDialog {

    @BindView(R.id.tv_version_name)
    TextView tvVersionName;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.btn_update)
    MainBlueConfirmButton btnUpdate;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    private OnUpdateVersionListener listener;
    private UpdateVersionBean bean;

    public UpdateVersionDialog(@NonNull Context context, UpdateVersionBean bean, OnUpdateVersionListener listener) {
        super(context);
        this.bean = bean;
        this.listener = listener;
    }

    @Override
    public int getLayout() {
        return R.layout.dialog_update_version;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        if (bean != null) {
            tvVersionName.setText(bean.getVersion_number());
            tvContent.setText(Html.fromHtml(bean.getMaincontent()));
            btnUpdate.setBtnEnabled(true);
            tvCancel.setOnClickListener(view -> dismiss());
            if (bean.getState() == 1) {
                this.setCanceledOnTouchOutside(false);
                tvCancel.setVisibility(View.GONE);
            }

            btnUpdate.setOnClickListener(view -> {
                if (listener != null) {
                    listener.update(bean.getDownload_link());
                    dismiss();
                }
            });
        }
    }

    @Override
    public void initData() {

    }

    /**
     * 拦截返回键
     */
    @Override
    public void onBackPressed() {
        if (bean.getState() == 1) {
            LogUtils.i("强制更新");
        } else {
            dismiss();
        }
    }

    public interface OnUpdateVersionListener {
        void update(String url);
    }
}
